import { Component, OnInit, Input } from '@angular/core'
import { Widget } from '../../model'
import { JsonApiService } from 'app/core/api/json-api.service'

declare var layui: any

@Component({
  selector: 'inventory-factory-details',
  templateUrl: './inventory-factory-details.html',
  styleUrls: ['./inventory-factory-details.css']
})
export class InventoryFactoryDetailsComponent implements OnInit, Widget {
  @Input() componentData: any
  companyIds: any = '' // 选中公司
  dateTime: any = '' // 实时时间
  tableData: any = [] //列表数据
  treeTable: any = null
  isShowModal: boolean = false //常用项目弹窗
  loading: boolean = false
  constructor(private jsonApiService: JsonApiService) { }
  initLayui() {
    const that = this
    layui.use(function () {
      that.treeTable = layui.treeTable
      that.treeTable.render({
        elem: '#inventory-factory-details-table',
        data: that.tableData,
        height: '430px',
        tree: {
          view: {
            showIcon: false,
            dblClickExpand: false
          }
        },
        cols: [
          [
            { field: 'name', title: '厂区名称', fixed: 'left', minWidth: 150 },
            { field: 'num', title: '主机良品库存', minWidth: 120 }
          ]
        ],
        page: false
      })
      // 树形表格单击展开
      that.treeTable.on('row(inventory-factory-details-table)', data => {
        const expandFlag = !data.data.LAY_EXPAND
        const index = data.index
        that.treeTable.expandNode('inventory-factory-details-table', {
          index,
          expandFlag
        })
      })
    })
  }
  ngOnInit() {
    this.companyIds = this.componentData.componentsConfig
    this.initLayui()
    this.refresh()
  }
  requestData() {
    const that = this
    let postParam = {
      companyIds: this.companyIds === 'default' ? '' : this.companyIds
    }
    this.loading = true
    this.jsonApiService.http_obj('control/station/inventory/company/inventoryCompanyDetail.html', postParam, false).subscribe(data => {
      this.tableData = []
      this.loading = false
      if (data.success) {
        this.tableData = data.data || []
        that.treeTable.reloadData('inventory-factory-details-table', {
          data: that.tableData
        })
      }
    })
  }
  //刷新获取当前时间
  refresh() {
    this.dateTime = common.getNowTime()
    this.requestData()
  }

  // 在组件销毁之前调用
  ngOnDestroy() { }
  //打开弹窗
  openClick() {
    this.isShowModal = true
  }
  //关闭弹窗
  closeDialog(data) {
    this.isShowModal = data.isShowModal
    const refresh = data.isRefresh
    if (refresh) {
      this.companyIds = data.companyIds
      this.initLayui()
      this.refresh()
    }
  }
}
